<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// 获取路由信息
const route = useRoute()
const $router = useRouter()

// 获取所有路由项（用于侧边栏）
import router from '../router'
const routes = computed(() => router.options.routes.filter(route => route.name))

// 当前激活的路由
const activeRoute = computed(() => route.name)
</script>

<template>
  <div class="app-container">
    <!-- 侧边栏 -->
    <aside class="sidebar">
      <div class="sidebar-header">
        <h2>商品管理系统</h2>
      </div>
      <nav class="sidebar-nav">
        <ul>
          <li 
            v-for="route in routes" 
            :key="route.path"
            :class="{ active: activeRoute === route.name }"
            @click="$router.push(route.path)"
          >
            <span class="nav-icon">{{ route.meta.icon }}</span>
            <span class="nav-title">{{ route.meta.title }}</span>
          </li>
        </ul>
      </nav>
    </aside>

    <!-- 主内容区域 -->
    <main class="main-content">
      <!-- 顶部导航栏 -->
      <header class="main-header">
        <h1>{{ routes.find(r => r.name === activeRoute)?.meta.title || '商品管理' }}</h1>
        <div class="user-info">
          <span class="user-avatar">👤</span>
        </div>
      </header>
      
      <!-- 路由视图 -->
      <div class="content-wrapper">
        <router-view />
      </div>
    </main>
  </div>
</template>

<style scoped>
/* 确保父容器没有边距 */
:deep(.app-container) {
  margin: 0;
  padding: 0;
}

.app-container {
  display: flex;
  height: 100vh;
  background-color: #f5f7fa;
  margin: 0;
  padding: 0;
}

/* 重置全局样式 */
:global(body),
:global(html) {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

:global(#app) {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100vh;
}

/* 侧边栏样式 */
.sidebar {
  width: 250px;
  background-color: #2c3e50;
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
  margin-left: 0;
  padding-left: 0;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid #34495e;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.sidebar-nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.sidebar-nav li {
  padding: 15px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: all 0.3s ease;
}

.sidebar-nav li:hover {
  background-color: #34495e;
}

.sidebar-nav li.active {
  background-color: #1abc9c;
}

.nav-icon {
  font-size: 16px;
}

.nav-title {
  font-size: 14px;
}

/* 主内容区域样式 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.main-header {
  background-color: white;
  padding: 0 25px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.main-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
  color: #333;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: #1abc9c;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.content-wrapper {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f0f2f5;
}
</style>